package com.aaa.java15;

import java.io.Serializable;
import java.util.*;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/7/19 8:57
 * @description：
 * @modified By：
 * @version:
 */
public class Person  implements Serializable {

    private static final long serialVersionUID = -3806202872584863128L;
    private int id;
    private String name;
    private int age;
    private boolean sex;

    public Person() {
    }

    public Person(int id, String name, int age, boolean sex) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.sex = sex;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Person person = (Person) o;
        return id == person.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }


    public static void main(String[] args) {
        //实例化一个map集合
        Map<Person,Person> map= new HashMap<>();
        //创建六个人
        Person person1= new Person(1,"杨过",20,true);
        Person person2= new Person(2,"小龙女",20,false);
        Person person3= new Person(3,"郭靖",20,true);
        Person person4= new Person(4,"黄蓉",20,false);
        Person person5= new Person(5,"张无忌",20,true);
        Person person6= new Person(6,"赵敏",20,false);
        //封装到map集合中
        map.put(person1,person2);
        map.put(person3,person4);
        map.put(person5,person6);
        //显示所有
        System.out.println("删除之前");
        System.out.println(map);
        //遍历map集合
        //方式一：快速for  Entry
        for (Map.Entry<Person, Person> personEntry : map.entrySet()) {
            System.out.println("key:"+personEntry.getKey()+"value："+personEntry.getValue());
        }
        System.out.println("===================");
        //方式二：快速for  keySet
        for (Person person : map.keySet()) {
            System.out.println("key:"+person+"|| value:"+map.get(person));

        }
        //方式三：通过迭代器
        System.out.println("=================");
        Set<Map.Entry<Person, Person>> entrySet = map.entrySet();
        Iterator<Map.Entry<Person, Person>> iterator = entrySet.iterator();
        while (iterator.hasNext()){
            Map.Entry<Person, Person> personEntry = iterator.next();
            System.out.println("key:"+personEntry.getKey()+"|| value："+personEntry.getValue());

        }


        System.out.println("删除之后");

        Person guojing=new Person(3,"郭靖",20,true);

        //根据key进行删除
         map.remove(guojing);

        for (Map.Entry<Person, Person> personEntry : map.entrySet()) {
            System.out.println("key:"+personEntry.getKey()+"value："+personEntry.getValue());
        }
    }



}
